package com.luyu.temp;

import com.luyu.tenant.TenantContextHolder;
import com.luyu.util.RedisKeyUtil;
import com.luyu.util.UserContextUtil;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class TempUserInfoUtil {
    @Resource
    private  RedisTemplate redisTemplate;

    public     TempUserInfo getTempUserInfo(){
        Object obj = redisTemplate.opsForHash().get(RedisKeyUtil.getTempUserInfoKey(), UserContextUtil.getUserName());
        TempUserInfo info = TempUserInfo.class.cast(obj);
        return info;
    }

    public    void setTempUserInfo(TempUserInfo userInfo){
        redisTemplate.opsForHash().put(RedisKeyUtil.getTempUserInfoKey(),userInfo.getUserName(),userInfo);
    }


    public TempUserInfo getTempUserInfo(String userName,Long tenantId){
        Object obj = redisTemplate.opsForHash().get(RedisKeyUtil.generateTempUserInfoKey(userName,tenantId),userName);
        if (obj == null) {
            return null;
        }
        TempUserInfo info = TempUserInfo.class.cast(obj);
        return info;
    }

    public Boolean remove(String userName) {
        Boolean res=true;
        String tenantId = TenantContextHolder.getTenantId().toString();
        if (redisTemplate.hasKey(tenantId)&&redisTemplate.opsForHash().hasKey(tenantId,userName)){
            redisTemplate.opsForHash().delete(tenantId,userName );
        }
        return res;
    }
}
